//
//  DataModelController.m
//  JiaoWuGuanLi
//
//  Created by Zinzie on 14-2-10.
//  Copyright (c) 2014年 Zinzie. All rights reserved.
//

#import "DataModelController.h"
#import "DataModel.h"
#import "TFHpple.h"
#import "MainInterfaceAppDelegate.h"
#import "Score.h"
#import "CoreData+MagicalRecord.h"
#import "MainInterfaceAppDelegate.h"
#import "Student.h"

@interface DataModelController ()
@property(nonatomic,strong)NSString *viewState;
@property(nonatomic,strong)NSMutableArray *chengJiCache;
@property(nonatomic,strong)NSString *urlScore;
@end
@implementation DataModelController

-(void)setYanZheng:(NSString *)yanZheng{
    if (_yanZheng!=yanZheng) {
        _yanZheng=yanZheng;
        dispatch_queue_t only=dispatch_queue_create("only", NULL);
        dispatch_async(only, ^{
            [self acquireViewStare];
        });
    }
}

-(void)setChengJiList:(NSMutableArray *)chengJiList{
    @synchronized(_chengJiList){
        _chengJiList = [chengJiList mutableCopy];
        NSLog(@"%@",self.termOfYear);
        if (self.termOfYear) {
            NSMutableIndexSet *set=[[NSMutableIndexSet alloc]init];
            for (DataModel *modals in _chengJiList) {
                if (![modals.year isEqualToString:self.scoreOfYear]||![modals.term isEqualToString:self.termOfYear]) {
                    [set addIndex:[self.chengJiList indexOfObject:modals]];
                }
            }
            [self.chengJiList removeObjectsAtIndexes:set];
        }

        
        else if (self.scoreOfYear) {
            NSLog(@"score of year = %@",self.scoreOfYear);
        NSMutableIndexSet *set=[[NSMutableIndexSet alloc]init];
        for (DataModel *modals in _chengJiList) {
            if (![modals.year isEqualToString:self.scoreOfYear]) {
                [set addIndex:[self.chengJiList indexOfObject:modals]];
                }
            }
        [self.chengJiList removeObjectsAtIndexes:set];
            }
        
            else{
                _chengJiList = [chengJiList mutableCopy];
                NSLog(@"chengJiList count %d",_chengJiList.count);
                }
    }
}

-(id)init{
    if (self = [super init]) {
        self.chengJiList = [[NSMutableArray alloc]init];
        self.chengjiListAll=[[NSMutableArray alloc]init];
        [self presentScoreInDataModel];
        self.refreshError = NO;
        self.loginWrong = NO;
        NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
        [AFHTTPRequestOperationManager manager].responseSerializer.stringEncoding=enc;
        [AFHTTPRequestOperationManager manager].requestSerializer.stringEncoding = enc;
        [AFHTTPRequestOperationManager manager].responseSerializer=[AFHTTPResponseSerializer serializer];
        [AFHTTPRequestOperationManager manager].requestSerializer = [AFHTTPRequestSerializer serializer];
//        [self jiaZaiChengJi];

        return self;
    }
    return nil;
}

-(void)acquireViewStare{
    [[AFHTTPRequestOperationManager manager] GET:@"http://gdjwgl.bjut.edu.cn/default2.aspx" parameters:nil
             success:^(AFHTTPRequestOperation *operation, id responseObject) {
                 NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
                 
                 NSData *data=responseObject;
                 NSString *transStr=[[NSString alloc]initWithData:data encoding:enc];
                 
                 NSString *utf8HtmlStr = [transStr stringByReplacingOccurrencesOfString:@"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">" withString:@"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"];
                 NSData *htmlDataUTF8 = [utf8HtmlStr dataUsingEncoding:NSUTF8StringEncoding];
                 TFHpple *xpathParser = [[TFHpple alloc]initWithHTMLData:htmlDataUTF8];
                 NSArray *elements  = [xpathParser searchWithXPathQuery:@"//input[@name='__VIEWSTATE']"];
                 
                 // Access the first cell
                 NSUInteger count=[elements count];
                 for (int i=0; i<count; i++) {
                     TFHppleElement *element = [elements objectAtIndex:i];
                     self.viewState=[element objectForKey:@"value"];
                     //                  [self huoDevs];
                     NSLog(@"1提取到得viewstate为%@",self.viewState);
                     [self logins];
                     
                     
                 }
             } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                 NSLog(@"view state Error: %@", [error debugDescription]);
             }];//获取登陆后的网页
    //    NSLog(@"1提取到得viewstate为%@",viewstates);
    //    self.viewState=viewstates;
    NSLog(@"2提取到得viewstate为%@",self.viewState);
    
}

-(void)logins{
    MainInterfaceAppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    NSString *dengLuXueHao = myDelegate.XueHao;
    NSPredicate *predicateStudent = [NSPredicate predicateWithFormat:@"xueHao == %@",dengLuXueHao];
    NSArray *StudentArray =[Student MR_findAllWithPredicate:predicateStudent];
    Student *currentUser =  [StudentArray lastObject];
    NSDictionary *parameters = @{@"__VIEWSTATE":self.viewState,@"txtUserName":currentUser.xueHao,@"TextBox2":currentUser.miMa,@"txtSecretCode":self.yanZheng,@"RadioButtonList1":@"学生",@"Button1":@""};
    [[AFHTTPRequestOperationManager manager] POST:@"http://gdjwgl.bjut.edu.cn/default2.aspx" parameters:parameters
              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                  NSURL *denglu=[NSURL URLWithString:@"http://gdjwgl.bjut.edu.cn/default2.aspx"];
                  if ([operation.response.URL isEqual:denglu]) {
//                      [self miMaCuoWu];
                      NSLog(@"登陆错误");
                      self.loginWrong = YES;
                      [[NSNotificationCenter defaultCenter] postNotificationName:@"ShuXinScoreOK" object:self];
                  }else{
                      NSLog(@"登陆成功");
                      self.loginWrong = NO;
                      NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                      [defaults setBool:YES forKey:@"online"];
                      self.viewState = nil;
                      [self refreshModal];
                     // NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
                      
                      //NSData *data=responseObject;
                      //NSString *tansData=[[NSString alloc]initWithData:data encoding:enc];
                      
                      //                      NSLog(@"转化过的string为%@",tansData);
                      
                      NSLog(@"biaodantijiaochenggong:%ld，%@",(long)operation.response.statusCode,operation.responseString);//提交表单
                     // NSString *utf8HtmlStr = [tansData stringByReplacingOccurrencesOfString:@"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">" withString:@"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"];
//                      NSData *htmlDataUTF8 = [utf8HtmlStr dataUsingEncoding:NSUTF8StringEncoding];
//                      [self anayseLoginData:htmlDataUTF8];
                  }
              } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                  NSLog(@"login Error: %@", @"???");
              }];
}


-(void)jiaZaiChengJi{//第一次加载
    [self getViewStateInScorePage];

}
-(void)getViewStateInScorePage{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.stringEncoding=enc;
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.requestSerializer.stringEncoding = enc;
    MainInterfaceAppDelegate *mainDele=[[UIApplication sharedApplication]delegate];
    NSString *uName=mainDele.userName;
    NSString *zhangHao=mainDele.XueHao;
    if(uName!=nil&&zhangHao!=nil){
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSDictionary *parameters2 = @{@"xh":zhangHao,@"xm":uName,@"gnmkdm":@"N121605"};
            //        [manager GET:@"http://172.21.96.64/xscjcx.aspx?xh=11024132&xm=%D5%C5%C8%AB%C5%F4&gnmkdm=N121605" parameters:nil
            [manager GET:@"http://gdjwgl.bjut.edu.cn/xscjcx.aspx?" parameters:parameters2
                 success:^(AFHTTPRequestOperation *operation, id responseObject) {
                     //              NSLog(@"huoqushuju: %ld",(long)operation.response.statusCode);
                     //                  NSLog(@"数据：%d",operation.response.statusCode);
                     
                     
                     NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
                     
                     NSData *data=responseObject;
                     NSString *transData=[[NSString alloc]initWithData:data encoding:enc];
                     
                     NSURL *urlLinshi=operation.request.URL;
                     self.urlScore=urlLinshi.absoluteString;
                     

                     
                     NSString *utf8HtmlStr = [transData stringByReplacingOccurrencesOfString:@"<meta content=\"text/html; charset=gb2312\" http-equiv=\"Content-Type\">" withString:@"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"];
                     NSData *htmlDataUTF8 = [utf8HtmlStr dataUsingEncoding:NSUTF8StringEncoding];
                     TFHpple *xpathParser = [[TFHpple alloc]initWithHTMLData:htmlDataUTF8];
                     //                  NSArray *elements  = [xpathParser searchWithXPathQuery:@"//table[@class='datelist']/tr[2]/td"];
                     NSArray *elements  = [xpathParser searchWithXPathQuery:@"//input[@name='__VIEWSTATE']"];
                     
                     
                     // Access the first cell

                    TFHppleElement *element = [elements objectAtIndex:0];
                    self.viewState=[element objectForKey:@"value"];
                    NSLog(@"1提取到得viewstate为%@",self.viewState);
                         //                      NSDictionary *parameters = @{@"__EVENTTARGET":@"",@"__EVENTARGUMENT":@"",@"__VIEWSTATE":self.viewState,@"btn_zcj": @"历年成绩"};
                    [self refreshModal];
                 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                     NSLog(@"Error: %@", [error debugDescription]);
                     
                     self.refreshError = YES;
                     [[NSNotificationCenter defaultCenter] postNotificationName:@"ShuXinScoreOK" object:self];
                     
                     
                     
                 }];//获取登陆后的网页
        });
        
    }
    
    
    
}

-(void)refreshModal{//更新
    if(self.viewState!=nil&&self.urlScore!=nil){
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.stringEncoding=enc;
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.requestSerializer.stringEncoding = enc;
    NSDictionary *parameters = @{@"__VIEWSTATE":self.viewState,@"ddlXN":@"",@"ddlXQ":@"",@"ddl_kcxz":@"",@"btn_zcj": @"历年成绩"};
    [manager POST:self.urlScore parameters:parameters
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
              NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
              
              NSData *data=responseObject;
              NSString *transStr=[[NSString alloc]initWithData:data encoding:enc];
              
              NSLog(@"biaodantijiaochenggong:%ld，%@",(long)operation.response.statusCode,transStr);//提交表单
              NSString *utf8HtmlStr = [transStr stringByReplacingOccurrencesOfString:@"<meta content=\"text/html; charset=gb2312\" http-equiv=\"Content-Type\">" withString:@"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"];
              NSData *htmlDataUTF8 = [utf8HtmlStr dataUsingEncoding:NSUTF8StringEncoding];
              [self arrangeData:htmlDataUTF8];
                       } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              NSLog(@"1231231Error: %@", [error description]);
                           self.refreshError = YES;
                           [[NSNotificationCenter defaultCenter] postNotificationName:@"ShuXinScoreOK" object:self];

          }];
        
    }else{
        [self getViewStateInScorePage];
    }
    

}
-(void)arrangeData:(NSData*)scoreData{
    [self.chengjiListAll removeAllObjects];
    TFHpple *xpathParser = [[TFHpple alloc]initWithHTMLData:scoreData];
    NSArray *elements  = [xpathParser searchWithXPathQuery:@"//table[@class='datelist']/tr/td"];
    // Access the first cell
    NSUInteger count=[elements count];
    [self.chengJiCache removeAllObjects];
    for (int i=0; i<count; i++) {
        TFHppleElement *element = [elements objectAtIndex:i];
        // Get the text within the cell tag
        NSString *content = [element text];
        if(content==nil){
            content=@"没有值";
        }
        [self.chengJiCache addObject:content];
        
        //                                    NSString *ta=[element tagName];
        //                                    NSLog(@"成绩为%@%@",content,ta);
    }
    
    //Get all the cells of the 2nd row of the 3rd table
    long countCache=[self.chengJiCache count];
    if(countCache>15){
        NSMutableIndexSet *indexes = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 15)];
        [self.chengJiCache removeObjectsAtIndexes:indexes];
        [self.chengJiList removeAllObjects];
        for(int i=0; i<(countCache-15)/15;i++){
            //                                    NSLog(@"%@",[self.chengJiCache objectAtIndex:i]);
            DataModel *scoreModal = [[DataModel alloc]initWithName:[self.chengJiCache objectAtIndex:3+i*15]
                                                              mark:[self.chengJiCache objectAtIndex:8+i*15]
                                                            jidian:[self.chengJiCache objectAtIndex:7+i*15]
                                                         attribute:[self.chengJiCache objectAtIndex:4+i*15]
                                                            number:[self.chengJiCache objectAtIndex:2+i*15]
                                                    chongXiuBiaoJi:[self.chengJiCache objectAtIndex:14+i*15]
                                                              year:[self.chengJiCache objectAtIndex:0+i*15]
                                                              term:[self.chengJiCache objectAtIndex:1+i*15]
                                                            xueFen:[self.chengJiCache objectAtIndex:6+i*15]
                                                           xueYuan:[self.chengJiCache objectAtIndex:12+i*15]];

            if ([[self.chengJiCache objectAtIndex:14+i*15] isEqualToString:@"重修"]) {
                NSLog(@"found a 重修");
                NSMutableArray *itemNeedToDelete = [[NSMutableArray alloc]init];
                for (DataModel *model in self.chengjiListAll) {
                    if([model.name isEqualToString:[self.chengJiCache objectAtIndex:3+i*15]]){
                        NSLog(@"%@ just removed",model.name);
                        [itemNeedToDelete addObject:@([self.chengjiListAll indexOfObject:model])];
                    }
                }
                if (itemNeedToDelete.count!=0) {
                    for (NSNumber *index in itemNeedToDelete) {
                        [self.chengjiListAll removeObjectAtIndex:[index integerValue]];
                    }
                }
                
            }
            [self addchengjiListWithChengji:scoreModal];
            //NSLog(@"重修标记为%@",[self.chengJiCache objectAtIndex:14+i*15]);
        }
        
        
        MainInterfaceAppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
        NSString* dengLuXueHao=myDelegate.XueHao;
        self.haveNewData = NO;
        if ([self localDataNeedToChangeToChengJiListAll:self.chengjiListAll]==YES) {
        self.haveNewData = YES;
            
            NSPredicate *classesOfStudent = [NSPredicate predicateWithFormat:@"ofWho.xueHao == %@",dengLuXueHao];
            for (Score *score in [Score MR_findAllWithPredicate:classesOfStudent]) {
                [score MR_deleteEntity];
            }
            NSPredicate *predicateStudent = [NSPredicate predicateWithFormat:@"xueHao == %@",dengLuXueHao];
            NSArray *StudentArray =[Student MR_findAllWithPredicate:predicateStudent];
            NSLog(@"找到%d个当前学生！！",StudentArray.count);
                Student *studentNow = [StudentArray lastObject];
            for (DataModel *scoreModal in self.chengjiListAll) {
                Score *scoreMagicModel = [Score MR_createEntity];
                scoreMagicModel.mingCheng = scoreModal.name;
                scoreMagicModel.chengJi = scoreModal.mark;
                scoreMagicModel.jiDian = scoreModal.jidian;
                scoreMagicModel.shuXing = scoreModal.attribute;
                scoreMagicModel.bianHao = scoreModal.number;
                scoreMagicModel.xueNian = scoreModal.year;
                scoreMagicModel.xueQi = scoreModal.term;
                scoreMagicModel.xueFen = scoreModal.xueFen;
                scoreMagicModel.xueYuan = scoreModal.xueYuan;
                scoreMagicModel.ofWho = studentNow;
                NSLog(@"储存数据的登录学号为 %@",scoreMagicModel.mingCheng);
            }
        [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
            if (success) {
                NSLog(@"成绩储存成功啦啦啦啦啦啦");
            }else{
                NSLog(@"成绩储存失败啦啦啦啦啦");
            }
        }];
        self.chengJiList=[self.chengjiListAll mutableCopy];
            self.refreshError = NO;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ShuXinScoreOK" object:self];
        }
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ShuXinScoreOK" object:self];
    [self presentScoreInDataModel];
    self.refreshError = NO;

}

-(void)presentScoreInDataModel{//将数据库中的score转为datamodel格式的array，并赋值给self.chengjilist
    MainInterfaceAppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    NSString* dengLuXueHao = myDelegate.XueHao;
    NSPredicate *scoreOfStudent = [NSPredicate predicateWithFormat:@"ofWho.xueHao == %@",dengLuXueHao];
    NSArray *scoreArray =[Score MR_findAllWithPredicate:scoreOfStudent];
    NSMutableArray *dataModelArray = [[NSMutableArray alloc]init];
    for (Score *scoreInScore in scoreArray) {
        DataModel *new = [[DataModel alloc]init];
        new.name = scoreInScore.mingCheng;
        new.mark = scoreInScore.chengJi;
        new.jidian =scoreInScore.jiDian;
        new.attribute = scoreInScore.shuXing;
        new.number = scoreInScore.bianHao;
        new.year  = scoreInScore.xueNian;
        new.term = scoreInScore.xueQi;
        new.xueFen = scoreInScore.xueFen;
        new.xueYuan = scoreInScore.xueYuan;
        [dataModelArray addObject:new];
    }
    self.chengjiListAll = dataModelArray;
    self.chengJiList = [self.chengjiListAll mutableCopy];
    NSLog(@"init chengjiList count %d",self.chengJiList.count);
}

-(BOOL)localDataNeedToChangeToChengJiListAll:(NSMutableArray *)array{
    MainInterfaceAppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    NSString* dengLuXueHao = myDelegate.XueHao;
    NSPredicate *scoreOfStudent = [NSPredicate predicateWithFormat:@"ofWho.xueHao == %@",dengLuXueHao];
    NSArray *scoreArray =[Score MR_findAllWithPredicate:scoreOfStudent];
    NSLog(@"scoreArray.count == %d",scoreArray.count);
    if (scoreArray.count==0) {
        NSLog(@"scoreArray.count==0");
        return YES;
    }else {
        if(array.count==0){
            return NO;
        }else{
            for (DataModel *scoreModel in array) {
                NSPredicate *everyScore= [NSPredicate predicateWithFormat:@"ofWho.xueHao == %@ AND mingCheng == %@ AND bianHao == %@",dengLuXueHao,scoreModel.name,scoreModel.number];
                if ([Score MR_findAllWithPredicate:everyScore].count==0) {
                    NSLog(@"find sth new");
                    return YES;
                }
            }
            return NO;
        }
    }
}
-(NSUInteger)countOfChengJiList{
    return [self.chengJiList count];
}

-(DataModel *)objectInChengJiListAtIndex:(NSUInteger)index{
    return [self.chengJiList objectAtIndex:index];
}

-(void)addchengjiListWithChengji:(DataModel *)chengJi{
    [self.chengjiListAll addObject:chengJi];
}
-(NSMutableArray *)chengJiCache{
    if(!_chengJiCache){
        _chengJiCache=[[NSMutableArray alloc]init];
    }
    return _chengJiCache;
}
-(void)removeAllObjects{
//    [self.chengjiListAll removeAllObjects];
    [self.chengJiList removeAllObjects];
}
-(void)setScoreOfYear:(NSString *)scoreOfYear{
    if (_scoreOfYear!=scoreOfYear) {
        _scoreOfYear=scoreOfYear;
        self.chengJiList=[self.chengjiListAll mutableCopy];

    }
}

-(void)setTermOfYear:(NSString *)termOfYear{
    if (_termOfYear != termOfYear) {
        _termOfYear=termOfYear;
        self.chengJiList=[self.chengjiListAll mutableCopy];
        //        NSMutableIndexSet *set=[[NSMutableIndexSet alloc]init];
        //        for (DataModel *modals in self.chengJiList) {
        //            if (![modals.year isEqualToString:scoreOfYear]) {
        //                [set addIndex:[self.chengJiList indexOfObject:modals]];
        //            }
        //        }
        //        [self.chengJiList removeObjectsAtIndexes:set];
    }
}

-(NSString *)standardGPA{
    
        _standardGPA=[[NSString alloc]init];
        double chengji = 0.00;
        double xuefen = 0.00;
        for (long i = [self.chengJiList count]-1; i>=0; i--) {
            DataModel *plus = [self.chengJiList objectAtIndex:i];
            NSMutableArray *attTest = [[NSMutableArray alloc]initWithCapacity:15];
            for (NSString *attString in attTest) {
                if (attString!=plus.attribute) {
                    [attTest addObject:plus.attribute];
                    NSLog(@"nnnnnnnnnew课程属性为%@",plus.attribute);
                }
            }
            if ([plus.attribute isEqualToString:@"公共基础必修课"]||[plus.attribute isEqualToString:@"学科基础必修课"]||
                [plus.attribute isEqualToString:@"专业认知"]||
                [plus.attribute isEqualToString:@"专业任选课"]||[plus.attribute isEqualToString:@"实践环节必修课"]||
                [plus.attribute isEqualToString:@"通识教育选修课"]||
                [plus.attribute isEqualToString:@"专业限选课"]||[plus.attribute isEqualToString:@"实践环节选修课"]||
                [plus.attribute isEqualToString:@"学科基础选修课"]) {
            
            chengji = chengji + [plus.mark doubleValue]*[plus.xueFen doubleValue];
            xuefen = xuefen + [plus.xueFen doubleValue];
                //NSLog(@"分数double为%.2f",[plus.mark doubleValue]);
                //NSLog(@"分数为%@,学分为%@,总成绩为%.2f,总学分为%.2f,属性为%@",plus.mark,plus.xueFen,chengji,xuefen,plus.attribute);
            }
        }
    double result = chengji / xuefen;
        NSLog(@"%@",[NSString stringWithFormat:@"%1f",result]);
        _standardGPA = [NSString stringWithFormat:@"%.2f",result];
        

    

    return _standardGPA;
}

-(NSString *)fourGPA{
    _fourGPA = [[NSString alloc]init];
    double chengji = 0.00;
    double xuefen = 0.00;
    double jidian = 0.00;
    for (int i = [self.chengJiList count]-1; i>0; i--){
        DataModel *gpa = [self.chengJiList objectAtIndex:i];
        if ([gpa.attribute isEqualToString:@"公共基础必修课"]||[gpa.attribute isEqualToString:@"学科基础必修课"]||
            [gpa.attribute isEqualToString:@"专业认知"]||
            [gpa.attribute isEqualToString:@"专业任选课"]||[gpa.attribute isEqualToString:@"实践环节必修课"]||
            [gpa.attribute isEqualToString:@"通识教育选修课"]||
            [gpa.attribute isEqualToString:@"专业限选课"]||[gpa.attribute isEqualToString:@"实践环节选修课"]||
            [gpa.attribute isEqualToString:@"学科基础选修课"]) {
            if ([gpa.mark doubleValue]>=85) {
                jidian = 4.0;
            }else if ([gpa.mark doubleValue] >=70){
                jidian = 3.0;
            }else if ([gpa.mark doubleValue]>=60){
                jidian = 2.0;
            }else{
                jidian = 0.0;
            }
            chengji = chengji + jidian * [gpa.xueFen doubleValue];
            xuefen = xuefen + [gpa.xueFen doubleValue];
            
        }
    }
    double result = chengji / xuefen;
    _fourGPA = [NSString stringWithFormat:@"%.2f",result];
    return _fourGPA;
}
@end
